{extend name="public:base" /}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-title" style="padding: 15px 15px 8px 15px; display: flex; justify-content: space-between">
                    {if ($all_complete_count < $all_answer_count && $question_status == 3) || $question_status == 0}
                    <div data-href="{:url('/index/Ai/allExec', ['id' => input('id')])}" class="btn btn-primary btn-lg exec">一键答题</div>
                    {else /}
                    <div></div>
                    {/if}
                    <div>
                        {if !$chromeDriverStatus}<a href="javascript: void(0)" onclick="startChromeDriver()" class="btn btn-primary btn-lg"><i class="iconfont icon-tool-bar-shutdown"></i> 启动ChromeDriver</a>{/if}
                        {if !$seleniumWebDriverStatus}<a href="javascript: void(0)" onclick="startSeleniumWebDriver()" class="btn btn-primary btn-lg" style="margin-left: 10px"><i class="iconfont icon-tool-bar-shutdown"></i> 启动SeleniumWebDriver</a>{/if}
                        {if session('id') == 1 && $running_plan_id > 0}
                        <div data-href="{:url('againRunning', ['id' => $running_plan_id])}" class="btn btn-primary btn-primary restart" style="margin-left: 5px"><i class="iconfont icon-zhongqi"></i> 未正常答题？重启运行 </div>
                        {/if}
                    </div>


                </div>


                <div class="ibox-content">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>单次生成答案数量</th>
                                <th>已提交问卷数</th>
                                <th>向AI发起请求</th>
                                <th>请求是否完成</th>
                                <th>自动答题程序</th>
                                <th>请求时间</th>
                                <th>完成时间</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {volist name="list" id="vo"}
                            <tr>
                                <td>{$vo.id}</td>
                                <td>{$vo.answer_count}</td>
                                <td>{$vo.complete_count}</td>
                                <td>{$vo.is_send == 1 ? '是' : '否'}</td>
                                <td>{$vo.is_complete == 1 ? '是' : '否'}</td>
                                <td>{:getStatusDesc($vo.status)}</td>
                                <td>{$vo.send_time|timeFormat}</td>
                                <td>{$vo.complete_time|timeFormat}</td>
                                <td>{$vo.create_time|timeFormat}</td>
                                <td class="project-actions">
                                    <a href="{:url('aiAnswerDetail', ['id' => $vo.id])}" class="btn btn-white btn-sm"><i class="iconfont icon-jurassic_log"></i> 作答详情
                                    </a>
                                    <a href="{:url('againAnswer', ['id' => $vo.id])}" class="btn btn-white btn-sm ajax-operate"><i class="iconfont icon-zhongxinhuida"></i> AI重新作答
                                    </a>
                                    {if $vo.status == 0 || ($vo.status == 3 && $vo.answer_count > $vo.complete_count)}
                                    <div data-href="{:url('/index/Ai/exec', ['id' => $vo.id])}" class="btn btn-white btn-sm exec"><i class="iconfont icon-tool-bar-shutdown"></i> 启动答题程序
                                    </div>
                                    {/if}
                                    <!--                                    <a href="{:url('delPlan', ['id' => $vo.id])}" class="btn btn-white btn-sm ajax-del"><i class="iconfont icon-trash"></i> 删除 </a>-->
                                </td>
                            </tr>
                            {/volist}
                            </tbody>
                        </table>
                        {if !empty($list)}{$list->render()}{/if}
                    </div>

                </div>
            </div>
        </div>

    </div>
</div>

{/block}

{block name="js"}
<script>
    function startChromeDriver() {
        layer.load(1, {
            shade: [0.8, '#393D49'] // 0.8透明度的背景色
        });
        layer.msg('正在启动ChromeDriver，请稍后...')
        $.post('/index/Ai/startChromeDriver')
        setTimeout(function () {
            location.reload()
        }, 2500)
    }

    function startSeleniumWebDriver() {
        layer.load(1, {
            shade: [0.8, '#393D49'] // 0.8透明度的背景色
        });
        layer.msg('正在启动SeleniumWebDriver，请稍后...')
        $.post('/index/Ai/startSeleniumWebDriver')
        setTimeout(function () {
            location.reload()
        }, 4000)
    }

    $('.exec').click(function () {
        var url = $(this).data("href");
        var chromeDriverStatus = `{$chromeDriverStatus}` ? true : false;
        var seleniumWebDriverStatus = `{$seleniumWebDriverStatus}` ? true : false;
        if (!chromeDriverStatus) {
            layer.msg('请启动ChromeDriver', {icon: 2});
            return false;
        }
        if (!seleniumWebDriverStatus) {
            layer.msg('请启动seleniumWebDriver', {icon: 2});
            return false;
        }

        swal({
            title: "您确认要执行该操作吗？",
            type: "warning",
            cancelButtonText: '取消',
            showCancelButton: true,
            confirmButtonColor: "#1BBF80",
            confirmButtonText: "确认",
            closeOnConfirm: false
        }, () => {
            // 加载层
            layer.load(2, {shade: [0.2, '#000']});

            startExec(url);
        });
        return false;
    })

    async function startExec(url) {
        try {
            let res = await $.getJSON(url);  // $.getJSON 是专门处理 JSON 的简便方法
            layer.msg(res.msg);
            if (res.result == 1) {
                setTimeout(function () {
                    window.location.reload();
                }, 500)
            } else {
                layer.closeAll();
                swal.close();
            }
        } catch (error) {
            layer.closeAll();
            swal.close();
            layer.msg('启动答题程序失败')
        }
    }


    $('.restart').click(function () {
        var url = $(this).data("href");

        swal({
            title: "您确认要执行该操作吗？",
            type: "warning",
            cancelButtonText: '取消',
            showCancelButton: true,
            confirmButtonColor: "#1BBF80",
            confirmButtonText: "确认",
            closeOnConfirm: false
        }, () => {
            // 加载层
            layer.load(2, {shade: [0.2, '#000']});

            startExec(url);
        });
        return false;
    })

    async function startExec(url) {
        try {
            let res = await $.getJSON(url);  // $.getJSON 是专门处理 JSON 的简便方法
            if (res.result == 1) {
                layer.closeAll();
                swal.close();
                layer.msg(res.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 2000)
            } else {
                layer.closeAll();
                swal.close();
                layer.msg(res.msg)
            }
        } catch (error) {
            console.log(error);
            layer.closeAll();
            swal.close();
            layer.msg('失败')
        }
    }
</script>{/block}

